﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;

namespace GhostStory.Model
{
    class Level
    {
        public enum Tile
        {
            T_EMPTY = 0,
            T_BLOCKED,
            T_TRAP,
            T_EGG,
            T_SPAWN,
            T_BLACKHOLE
        };

        public enum CurrentLevel
        {
            LEVEL_1 = 0,
            LEVEL_2,
            LEVEL_3
        };

        public const int g_levelWidth = 200;
        public const int g_levelHeight = 9;

        //the 2D array of tiles
        internal Tile[,] m_tiles = new Tile[g_levelWidth, g_levelHeight];
        private CurrentLevel m_CurrentLevel = CurrentLevel.LEVEL_1;
        private List<Vector2> m_spawnPos = new List<Vector2>();

        internal Level(CurrentLevel current)
        {
            m_CurrentLevel = current;
            GenerateLevel();
        }

        internal void setNextLevel()
        {
            if(m_CurrentLevel == CurrentLevel.LEVEL_1)
                m_CurrentLevel = CurrentLevel.LEVEL_2;
            else if (m_CurrentLevel == CurrentLevel.LEVEL_2)
                m_CurrentLevel = CurrentLevel.LEVEL_3;
        }

        internal void setFirstLevel()
        {
            m_CurrentLevel = CurrentLevel.LEVEL_1;
        }

        internal CurrentLevel getCurrentLevel()
        {
            return m_CurrentLevel;
        }

        private void GenerateLevel()
        {
            m_spawnPos.Clear();
            for (int x = 0; x < g_levelWidth; x++)
            {
                for (int y = 0; y < g_levelHeight; y++)
                {
                    m_tiles[x, y] = Tile.T_EMPTY; //set every tile to empty
 
                }



                //m_tiles[x, g_levelHeight - 1] = Tile.T_BLOCKED;

            }
            string[] lines = new string[0];
            if(m_CurrentLevel == CurrentLevel.LEVEL_1)
                lines = System.IO.File.ReadAllLines("level1.txt");
            if(m_CurrentLevel == CurrentLevel.LEVEL_2)
                lines = System.IO.File.ReadAllLines("level2.txt");
            if (m_CurrentLevel == CurrentLevel.LEVEL_3)
                lines = System.IO.File.ReadAllLines("level3.txt");

            // Display the file contents by using a foreach loop.
            //System.Console.WriteLine("Contents of WriteLines2.txt = ");
            foreach (string line in lines)
            {
                string[] ss = line.Split(',');
                if(ss.Length > 1)
                    m_tiles[Convert.ToInt32(ss[0]), Convert.ToInt32(ss[1])] = Tile.T_BLOCKED;
                if (ss.Length > 2)
                {
                    if(ss[2] == "1")
                        m_tiles[Convert.ToInt32(ss[0]), Convert.ToInt32(ss[1])] = Tile.T_EGG;
                    if (ss[2] == "2")
                    {
                        m_tiles[Convert.ToInt32(ss[0]), Convert.ToInt32(ss[1])] = Tile.T_SPAWN;
                        m_spawnPos.Add(new Vector2(Convert.ToInt32(ss[0]), Convert.ToInt32(ss[1])));
                    }
                    if (ss[2] == "3")
                    {
                        m_tiles[Convert.ToInt32(ss[0]), Convert.ToInt32(ss[1])] = Tile.T_BLACKHOLE;
                    }
                    
                }
                    
                //Console.WriteLine("\t" + line);
            }
        }

        public bool IsCollidingAt(Vector2 a_pos, Vector2 a_size, Level.Tile tile, ref int X, ref int Y)
        {

            Vector2 topLeft = new Vector2(a_pos.X - a_size.X / 2.0f, a_pos.Y - a_size.Y);
            Vector2 bottomRight = new Vector2(a_pos.X + a_size.X / 2.0f, a_pos.Y);


            for (int x = 0; x < g_levelWidth; x++)
            {
                for (int y = 0; y < g_levelHeight; y++)
                {

                    if (bottomRight.X < (float)x)
                        continue;
                    if (bottomRight.Y < (float)y)
                        continue;
                    if (topLeft.X > (float)x + 1.0f)
                        continue;
                    if (topLeft.Y > (float)y + 1.0f)
                        continue;

                    if (m_tiles[x, y] == tile)
                    {
                        X = x;
                        Y = y;
                        return true;
                    }
                }
            }


            return false;
        }

        public List<Vector2> getSpawn()
        {
            return m_spawnPos;
        }


    }
}
